package com.iweb.newa202104.controller;

import com.iweb.newa202104.dto.ResponseInputDTO;
import com.iweb.newa202104.enums.Err;
import com.iweb.newa202104.exception.CommonException;
import com.iweb.newa202104.service.ResponseService;
import com.iweb.newa202104.vo.ResultVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;

/**
 * @Author: LGZ
 * @Date: 2021/5/12 18:16
 * @Description:
 */
@Slf4j
@RestController
@RequestMapping("/response")
@CrossOrigin
public class ResponseController {

    @Autowired
    ResponseService responseService;

    @PostMapping("/add")
    public ResultVO add(HttpSession httpSession, @Validated @RequestBody ResponseInputDTO responseInputDTO) {
        return responseService.add(httpSession, responseInputDTO);
    }

    @GetMapping("/getAll")
    public ResultVO getAll(String questionId) {
        try {
            return responseService.getAll(Integer.parseInt(questionId));
        } catch (NumberFormatException e) {
            throw new CommonException(Err.PRAM_ERR);
        }
    }

    @GetMapping("/getMyResponseCounts")
    public ResultVO getMyResponseCounts(HttpSession httpSession) {
        return responseService.getMyResponseCounts(httpSession);
    }

    @GetMapping("/getMyResponse")
    public ResultVO getMyResponse(HttpSession httpSession) {
        return responseService.getMyResponse(httpSession);
    }

    @DeleteMapping("/delete")
    public ResultVO delete(HttpSession httpSession, String responseId) {
        try {
            return responseService.delete(httpSession, Integer.parseInt(responseId));
        } catch (NumberFormatException e) {
            throw new CommonException(Err.PRAM_ERR);
        }
    }

    @PostMapping("/giveLike")
    public ResultVO giveLike(@Validated @RequestBody ResponseInputDTO responseInputDTO) {
        return responseService.giveLike(responseInputDTO);
    }

}
